import twitter4j.*;

import java.text.ParseException;
import java.util.*;
import java.util.logging.Logger;

/**
 * Twitter application using Twitter4J
 */
public class Test {
    private final static Logger logger = Logger.getLogger(Test.class.getName());
    public static void main(String[] args) {
	    ArrayList<String> currentTrends = new ArrayList<String>();
        currentTrends = new Test().retrieveTrends(currentTrends);
        System.out.println(currentTrends);
        
        for(int i = 0; i < currentTrends.size(); i++){
        	System.out.println("");
        	System.out.println("New Trend: " + currentTrends.get(i));
        	retrieveLinks(currentTrends.get(i));
        }
    }
        
    public ArrayList<String> retrieveTrends(ArrayList<String> currentTrends) {
    	try {
    		Twitter twitter = new TwitterFactory().getInstance();
    	    Trends trends = twitter.getLocationTrends(23424977); //United States geolocation
    	    //System.out.println("U.S trends");
    	    for(Trend t: trends.getTrends()){
    	        String name = t.getName();
    	        //System.out.println(name);
    	        currentTrends.add(name);
    	    }
    	}catch (TwitterException te) {
          te.printStackTrace();
          System.out.println("Failed to get trends: " + te.getMessage());
          System.exit(-1);
      }
	    return currentTrends;
    }
    
  //retrieve tweets for one specific word

  public static void retrieve(String trend) {
      logger.info("Retrieving tweets...");
      Twitter twitter = new TwitterFactory().getInstance();
     // Query query = new Query("from:" +user);
      Query query = new Query(trend);
      query.setRpp(10);
      query.setSince("2012-01-01");
      try {
          QueryResult result = twitter.search(query);
          System.out.println("Count : " + result.getTweets().size()) ;
          for (Tweet tweet : result.getTweets()) {
              System.out.println("text : " + tweet.getText());
          }
      } catch (TwitterException e) {
          e.printStackTrace();
      }
      logger.info("done! ");
  }
    
    
    //retrieve tweets for one specific word that has links

    public static void retrieveLinks(String trend) {
        logger.info("Retrieving tweets...");
        Twitter twitter = new TwitterFactory().getInstance();
       // Query query = new Query("from:" +user);
        Query query = new Query(trend + " " + "filter:links");
        query.setRpp(10);
        query.setSince("2012-01-01");
        try {
            QueryResult result = twitter.search(query);
            System.out.println("Count : " + result.getTweets().size()) ;
            for (Tweet tweet : result.getTweets()) {
                System.out.println("text : " + tweet.getText());
            }
        } catch (TwitterException e) {
            e.printStackTrace();
        }
        logger.info("done! ");
    }
    
  //retrieve tweets for one specific word

//  public void retrieve() {
//      logger.info("Retrieving tweets...");
//      Twitter twitter = new TwitterFactory().getInstance();
//     // Query query = new Query("from:" +user);
//      Query query = new Query("Falcons");
//      query.setRpp(100);
//      query.setSince("2012-01-01");
//      try {
//          QueryResult result = twitter.search(query);
//          System.out.println("Count : " + result.getTweets().size()) ;
//          for (Tweet tweet : result.getTweets()) {
//              System.out.println("text : " + tweet.getText());
//          }
//      } catch (TwitterException e) {
//          e.printStackTrace();
//      }
//      logger.info("done! ");
//  }

    //retrieve tweets for a specific user

//    public void retrieve() {
//        logger.info("Retrieving tweets...");
//        Twitter twitter = new TwitterFactory().getInstance();
//        String user = "ladygaga";
//        Query query = new Query("from:"+user);
//        query.setRpp(10);
//        query.setSince("2012-01-01");
//        try {
//            QueryResult result = twitter.search(query);
//            System.out.println("Count : " + result.getTweets().size()) ;
//            for (Tweet tweet : result.getTweets()) {
//                System.out.println("text : " + tweet.getText());
//            }
//        } catch (TwitterException e) {
//            e.printStackTrace();
//        }
//        logger.info("done! ");
//    }
}
